<template>
  <section class="c-flex-column" :class="[!isSetPageBackground ? 'c-bg-white' : '']">
    <cust-title-com v-if="item.isShowTitle"
      :modelType="modelType"
      :marginSettings="marginSettings"
      :headLine="headLine"
      :titleName="item.title || showTitleDefaultName"
      :titleDesc="item.desc"
      :visible="item.visible"
      :showMore="item.isShowMore"
      :showMoreType="item.showMoreType"
      :launchWeapp="launchWeapp"
      @clickMore="clickMore"
    ></cust-title-com>
    <div class="c-ph24 c-pb34 c-pt20" :style="getVerticalSpacing(marginSettings) + getHorizontalSpacing(marginSettings)">
      <!-- 资料的单图显示 -->
      <div class="c-w100 c-text-hidden c-p c-bg-white" :style="getBorderRadius(borderRadius, '0.4rem')" @click="clickMore" v-if="modelType == 10 && (!item.items.type || item.items.type == 5)">
        <img :src="$addXossFilter(item.items.imgUrl, 'https://kposshzcdn.ckjr001.com/admin/material/9_material_admin/image/assets/i/wap/index/img_datum_mb6.png')" class="imgCloseBig c-w100 c-h c-pz7 c-p" />
      </div>
      <!-- 拼团、限时购、友情灌溉、砍价、搭配购 -->
      <common-market-item
        v-else-if="[6, 20, 27, 49, 62].includes(modelType)"
        :imageSetting="imageSetting"
        :marginSettings="marginSettings"
        :headLine="headLine"
        :uiStyleType="item.items.uiStyleType"
        :showCount="item.items.count"
        :visible="item.visible"
        :itemList="getMixMarketItemList(1)"
        :pageName="pageName"
        :orderBy="item.items.orderBy"
        :showMore="item.isShowMoreBottom"
        @clickMore="clickMore"
      ></common-market-item>
      <!-- 特惠专区 -->
      <template v-else-if="modelType == 28">
        <common-market-item
          :imageSetting="imageSetting"
          :uiStyleType="item.items.uiStyleType"
          :marginSettings="marginSettings"
          :headLine="headLine"
          :showCount="getMixMarketItemCount"
          :visible="item.visible"
          :itemList="getMixMarketItemList(2)"
          :pageName="pageName"
          :orderBy="item.items.orderBy"
          :showMore="0"
          @clickMore="clickMore"></common-market-item>
      </template>
      <!-- 其他 -->
      <common-prod-item
        v-else
        :borderRadius="borderRadius"
        :imageSetting="imageSetting"
        :marginSettings="marginSettings"
        :headLine="headLine"
        :visible="item.visible"
        :modelType="modelType"
        :itemList="item.items.prods"
        :pageName="pageName"
        :orderBy="item.items.orderBy"
        :showMore="item.isShowMoreBottom"
        @clickMore="clickMore"
      ></common-prod-item>
    </div>
  </section>
</template>
<script>
import custTitleCom from "@/components/templates/custIndexTemp/custTitleCom.vue";
import CommonMarketItem from "@/components/templates/commonMarketItem.vue";
import CommonProdItem from "@/components/templates/commonProdItem.vue";
import { utilJs } from "@/utils/common.js";
import { appRouterChange } from "@/mixin/appRouterPush.js";
import { custIndex } from "@/mixin/custIndex.js";
import { customStyle } from '@/utils/customStyle.js'
export default {
  mixins: [appRouterChange, customStyle, custIndex],
  components: {
    custTitleCom,
    CommonProdItem,
    CommonMarketItem
  },
  props: {
    modelType: {
      type: Number,
      default: 0
    },
    item: {
      type: Object,
      default: () => {
        return {}
      },
    },
    pageName: {//首页INDEX 预览页PREVIEW_INDEX 微页面MICRO_PAGE
      type: String,
      default: "INDEX"
    },
  },
  data() {
    return {
      ckFrom: global.ckFrom,
      isSetPageBackground: sessionStorage.getItem("isSetPageBackground") || 0,
      launchWeapp: null, // 存储组件标题查看更多跳转到小程序得数据
    };
  },
  computed: {
    borderRadius() {
      return this.item.styleParam && this.item.styleParam.borderRadius ? this.item.styleParam.borderRadius : null
    },
    imageSetting() {
      return this.item.styleParam && this.item.styleParam.imageSetting ? this.item.styleParam.imageSetting : null
    },
    marginSettings() {
      return this.item.styleParam && this.item.styleParam.marginSettings ? this.item.styleParam.marginSettings : null
    },
    headLine() {
      return this.item.styleParam && this.item.styleParam.headLine ? this.item.styleParam.headLine : null
    },
    showMoreType() {
      // 标题文本 36 富文本 37 专区 59 自定义分类 4 走item.items.showMoreType
      // 课程  专栏 测评 活动 知识套餐 词典 互动测评 ai测评 训练营 考试
      return [5, 7, 9, 16, 21, 26, 29, 30, 31, 45].includes(this.modelType) ? this.item.showMoreType : this.item.items.showMoreType;
    },
    showTitleDefaultName() {
      let modelTypeName = {
        5: '课程',
        7: '专栏',
        8: '问答',
        9: '测评',
        12: '商城',
        15: '课程预告',
        16: '活动',
        19: '打卡',
        21: '知识套餐',
        29: '互动测评',
        30: '语音测试',
        45: '考试',
        46: '练习',
        54: '直播间'
      }
      return modelTypeName[this.modelType]
    },
    getMarketType() {
      return function(type) {
        if (!type) return ''
        let map = {
          1: 'collageList',
          2: 'friendHelperList',
          3: 'flashSalesList',
          4: 'bargainList',
          6: 'collageList',
          49: 'bargainList',
          27: 'flashSalesList',
          20: 'friendHelperList',
          62: 'collocationBuy'
        }
        return map[type]
      }
    },
    getMixMarketItemList() {
      return function(type) {
        let list = [];
        if (type == 1) {
          this.item.items.prods.forEach(item => {
            item.marketType = this.getMarketType(this.modelType)
            list.push(item)
          })
        } else {
          this.item.items.list.forEach(item => {
            if (item.isChecked == false) return
            item.prods.forEach(it => {
              it.marketType = this.getMarketType(item.prodType)
              list.push(it)
            })
          });
        }
        return list
      }
    },
    getMixMarketItemCount() {
      let count = 0;
      this.item.items.list.forEach(item => {
        count += item.count;
      });
      return count
    }
  },
  methods: {
    // 自定义跳转更多
    customMore(moreInfo) {
      if (!moreInfo || (moreInfo && !moreInfo.navId)) return
      if (moreInfo.navId == "linkToMiniProgram") {
        utilJs.checkWxApp(); // 再次调用是为了跳转小程序返回后避免小程序环境值判断错误
        this.$nextTick(() => {
          // 微信需要判断是否是小程序环境 h5跳转采用组件跳转 小程序跳转使用跳转到小程序中间页面再点击跳转到其他小程序
          let appID = moreInfo.appID || (moreInfo.info ? moreInfo.info.appID : '');
          let wechatUrl = moreInfo.wechatUrl || (moreInfo.info ? moreInfo.info.wechatUrl : '');
          if (localStorage.getItem('isWxApp')) {
            let query = appID ? `?appId=${appID}&path=${encodeURIComponent(JSON.stringify(wechatUrl))}` : `?shortLink=${encodeURIComponent(wechatUrl)}`;
            utilJs.miniProgramGoTo('push', '/pages/home/goWxApp/index' + query);
          } else {
            if (!moreInfo || (moreInfo && !appID)) return
            this.launchWeapp = {
              appID: appID,
              wechatUrl: wechatUrl,
            }
          }
        })
      }
      this.commonModuleRouterPush(moreInfo)
    },
    clickMore() {
      if (this.pageName == 'PREVIEW_INDEX') {
        return;
      }
      let toPath = "";
      // 特殊字段判断课程组件设置自定义跳转
      if (this.modelType == 5 && this.item.courseRedirect && this.item.courseRedirect.redirectType && this.item.courseRedirect.redirectType == 4) {
        this.customMore(this.item.moreInfo);
        return
      }
      // 跳转到旧的商品分组列表
      if (this.item.redirect && this.item.redirect.type == 2) {
        toPath = `/homePage/productGroup/groupList?pgId=${this.item.redirect.group[0].pgId}`;
        this.iosAppRouteChange(toPath);
      } else if (this.item.redirect && this.item.redirect.type == 3) { // 跳转指定商品分组
        if (this.modelType == 12) { // 商城自定义跳转区别于其他产品组件
          this.customMore(this.item.items);
          return
        }
        toPath = this.cateUrl(this.item.redirect.group[0].level1CatId, this.item.redirect.group[0].level2CatId, this.item.redirect.group[0].level3CatId, this.item.redirect.group[0].groupName);
        this.iosAppRouteChange(toPath);
      } else if (this.item.redirect && this.item.redirect.type == 4) { // 非实体产品组件自定义跳转判断
        this.customMore(this.item.moreInfo);
      } else { // 默认根据产品类型跳转对应列表
        if (this.modelType == 5) {
          let sort = `&sortType=` + (!this.item.sortType ? 1 : this.item.sortType);
          toPath = "/homePage/course/courseList?type=0" + sort;
        } else if (this.modelType == 6) {
          toPath = "/homePage/collage/collageList";
          if (this.item && this.item.items && this.item.items.productType && [1, 2].includes(this.item.items.productType)) {
            toPath = `${toPath}?prodType=${this.item.items.productType == 1 ? 12 : 1}`
          }
        } else if (this.modelType == 8) {
          toPath = '/homePage/qa/answerLord';
        } else if (this.modelType == 7) {
          toPath = "/homePage/column/columnList";
        } else if (this.modelType == 9) {
          toPath = "/homePage/examination/examinationList";
        } else if (this.modelType == 10) {
          toPath = "/homePage/datum/datumList";
        } else if (this.modelType == 12) {
          toPath = "/mall/classification/classificationAll?cId=0";
        } else if (this.modelType == 15) {
          toPath = "/homePage/course/courseForenoticeQrcode?cId=" + this.item.items.prods[0].courseId;
        } else if (this.modelType == 16) {
          toPath = "/activity/activityIndex";
        } else if (this.modelType == 18) {
          toPath = "/homePage/live/liveList";
        } else if (this.modelType == 19) {
          toPath = "/homePage/clockIn/clockInList";
        } else if (this.modelType == 20) {
          toPath = "/homePage/task/taskList";
        } else if (this.modelType == 21) {
          toPath = "/homePage/package/packageList";
        } else if (this.modelType == 24) {
          toPath = "/homePage/reservation/reservationList";
        } else if (this.modelType == 25) {
          toPath = this.orderType == 0 ? `/homePage/distribution/distributionList` : `/homePage/distribution/distributionList?orderType=${this.item.items.type}`;
        } else if (this.modelType == 26) {
          toPath = "/homePage/word/wordList";
        } else if (this.modelType == 27) {
          toPath = "/homePage/flashSales/flashSalesList?isFlashSales=true";
          if (this.item && this.item.items && this.item.items.productType && [1, 2].includes(this.item.items.productType)) {
            toPath = `${toPath}&prodType=${this.item.items.productType == 1 ? 12 : 1}`
          }
        } else if (this.modelType == 28) {
          let collage = this.item.items.list[0] && this.item.items.list[0].isChecked ? this.item.items.list[0].productType : 0;
          let task = this.item.items.list[1] && this.item.items.list[1].isChecked ? this.item.items.list[1].productType : 0;
          let flash = this.item.items.list[2] && this.item.items.list[2].isChecked ? this.item.items.list[2].productType : 0;
          let bargain = this.item.items.list[3] && this.item.items.list[3].isChecked ? this.item.items.list[3].productType : 0;
          toPath = `/homePage/flashSales/flashSalesList?collage=${collage}&task=${task}&flash=${flash}&bargain=${bargain}`;
        } else if (this.modelType == 29) {
          toPath = "/homePage/mutuaTest/mutuaTestList";
        } else if (this.modelType == 30) {
          toPath = "/homePage/voiceTest/voiceTestList";
        } else if (this.modelType == 31) {
          toPath = "/homePage/camp/campList";
        } else if (this.modelType == 43) { // 面授课
          toPath = "/edu/eduList";
        } else if (this.modelType == 45) {
          toPath = "/homePage/testPaper/testList";
        } else if (this.modelType == 46) {
          toPath = "/homePage/itemBank/questionList";
        } else if (this.modelType == 47) {
          toPath = `/homePage/ebooks/ebooksList`;
        } else if (this.modelType == 49) {
          toPath = `/homePage/bargain/bargainList`;
          if (this.item && this.item.items && this.item.items.productType && [1, 2].includes(this.item.items.productType)) {
            toPath = `${toPath}?prodType=${this.item.items.productType == 1 ? 12 : 1}`
          }
        } else if (this.modelType == 54) {
          toPath = "/homePage/live/livePersonalList";
        } else if (this.modelType == 55) {
          toPath = "/homePage/circle/circleList";
        } else if (this.modelType == 60) { // 有价券
          toPath = "/member/myExchange/valuableCouponList";
        } else if (this.modelType == 62) {
          toPath = '/homePage/collocationBuy/collocationBuyList';
        } else if (this.modelType == 42) {
          toPath = "/homePage/privateClass/classList"
        }
        this.iosAppRouteChange(toPath);
      }
    },
  },
};
</script>
